<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\ZPituitaryUser;
use App\Models\ZZMed\ZUser;
use App\Services\ManageDataService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


/**
 *  分析统计
 * Class StatisticsReportController
 * @package App\Http\Controllers\Api
 */
class StatisticsReportController extends Controller
{

    protected $oneYearAgoDate;     //一年前的时间
    protected $thisMonthFirstDay;  //本月第一天
    protected $thisMonthLastDay;   //本月最后一天

    public function __construct()
    {
        $this->oneYearAgoDate = date("Y-m-01", strtotime("-1 year +1 month"));
        $this->thisMonthFirstDay = date("Y-m-01", time());
        $this->thisMonthLastDay = date('Y-m-d', strtotime("$this->thisMonthFirstDay +1 month -1 day"));
    }

    /**
     *  管理人数
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function getManageUserData(Request $request)
    {
        $params = $request->all();
        $rules = [
            'hosp_id' => 'required',
            'dept_id' => 'required',
        ];
        $validator = Validator::make($params, $rules);
        if ($validator->fails()) {
            return $this->error(42200, "参数错误");
        }

        $timeFrom = date("Y-m-01", strtotime("-1 year +1 month"));
        $now = date("Y-m-d", time());

        $date = ManageDataService::stuffTime($timeFrom, $now);
        $time = [];
        foreach ($date as $k => $v) {
            $time[$v] = [];
        }

        //取出一年内本院入组的所有患者
        $hospGroupData = ZPituitaryUser::getGroupUserList($params['hosp_id'], $params['dept_id'], false, false, 2);

        $qualityData = [];
        foreach ($hospGroupData as $k => $v) {
            $qualityData[$v['user_id']] = 0;
        }

        //管理人数统计处理
        $result = ManageDataService::stuffData($hospGroupData, $time, $qualityData);

        return $this->success($result);
    }

    /**
     * 垂体各疾病占比
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function getPituitaryPercent(Request $request)
    {
        $params = $request->all();
        $rules = [
            'hosp_id' => 'required',
            'dept_id' => 'required',
        ];
        $validator = Validator::make($params, $rules);
        if ($validator->fails()) {
            return $this->error(42200, "参数错误");
        }

        //获取本院入组的患者
        $allData = ZPituitaryUser::getGroupUserList($params['hosp_id'], $params['dept_id']);
        $userIdArray = array_column($allData, 'user_id');
        $count = count($allData);

        //获取垂体病例
        $diabetesData = ManageDataService::getPituitaryData($userIdArray);

        //整理数据
        $diabetesNum = ManageDataService::stuffPituitaryData($diabetesData, $count);

        return $this->success($diabetesNum);
    }

    /**
     * 计算年龄占比
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function getAgeProportion(Request $request)
    {
        $params = $request->all();
        $rules = [
            'hosp_id' => 'required',
            'dept_id' => 'required',
        ];
        $validator = Validator::make($params, $rules);
        if ($validator->fails()) {
            return $this->error(42200, "参数错误");
        }
        $allData = ZPituitaryUser::getGroupUserList($params['hosp_id'], $params['dept_id']);

        $ageData = ManageDataService::stuffAgeData($allData);

        return $this->success($ageData);
    }

    /**
     *  既往病史占比
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function getIllnessProportion(Request $request)
    {
        $params = $request->all();
        $rules = [
            'hosp_id' => 'required',
            'dept_id' => 'required',
        ];
        $validator = Validator::make($params, $rules);
        if ($validator->fails()) {
            return $this->error(42200, "参数错误");
        }
        //取出本院入组的所有患者
        $hospGroupData = ZPituitaryUser::getGroupUserList($params['hosp_id'], $params['dept_id']);
        $userIdArray = array_column($hospGroupData, 'user_id');

        //获取患者的病史数据
        $data = ManageDataService::getIllnessData($userIdArray);
        // 处理数据
        $illnessData = ManageDataService::stuffIllnessData($data,  count($userIdArray));

        return $this->success($illnessData);
    }

}